package edu.ucsd.ccdb.rightClickPopupUtil;

import java.awt.AWTException;
import java.awt.Component;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import edu.ucsd.irods.IRODSFileString;

public class RightClickPopupListener extends MouseAdapter {
	
	private JTree tree;
	private JPopupMenu popup;
	
	public RightClickPopupListener (JPopupMenu popup, JTree tree) {
	
		this.popup = popup;
		this.tree = tree;
	}
	
	@Override
	public void mousePressed (MouseEvent e ) {
		maybeShowPopup(e);
	}
	
	 public void mouseReleased(MouseEvent e) {
		maybeShowPopup(e);
	}
	 
     private void maybeShowPopup(MouseEvent e) {
     	

    	 if (tree.isSelectionEmpty() ) return;
     	
        if (e.isPopupTrigger() ) {
        	
//			Point pt = new Point (e.getComponent().getLocation());
//			SwingUtilities.convertPointToScreen(pt, e.getComponent());
			
	//    			robot.mouseMove( pt.x + e.getX() , pt.y + e.getY()  );
//		 	System.out.println ( tree.getRowForLocation(e.getX()  , e.getY() ) );
			
			int index = -1;
			if ( ( index = tree.getRowForLocation(e.getX(), e.getY()) ) != -1 ) { 
				tree.setSelectionRow( index  );
			}
			
	//    			robot.mouseMove( pt.x + e.getX() , pt.y + e.getY()  );
			
	//    			robot.mousePress(InputEvent.BUTTON1_MASK);
	//    			robot.mouseRelease(InputEvent.BUTTON1_MASK);
			System.out.println ("Simulate click");
		
        	
    		popup.show(e.getComponent(), e.getX(), e.getY());
    		
    		// validation on 'create new folder' disable or not
    		TreePath selectedPath = tree.getSelectionPath();
    		DefaultMutableTreeNode node = (DefaultMutableTreeNode)  selectedPath.getLastPathComponent() ;
    		
    		IRODSFileString file = (IRODSFileString) node.getUserObject();
    		
    		if ( !file.isDirectory() ) {
    			// disable the option'
    			JComponent createNewFolderComponent = (JComponent) popup.getComponent(9);
    			createNewFolderComponent.setEnabled(false);
    		}
    		else {
    			JComponent createNewFolderComponent = (JComponent) popup.getComponent(9);
    			createNewFolderComponent.setEnabled(true);
    		}
    		
    		// validation on 'delete all CCDB tags disable or not
    		if (file.isDirectory() ) {
    			JComponent removeChildrenComponent = (JComponent) popup.getComponent(11);
    			removeChildrenComponent.setEnabled(true);
    		}
    		else {
    			JComponent removeChildrenComponent = (JComponent) popup.getComponent(11);
    			removeChildrenComponent.setEnabled(false);
    		}
    		
    		// validation on 'delete all Ontology tags' disable or not
    		if (file.isDirectory()) {
    			JComponent removeChildrenComponent = (JComponent) popup.getComponent(13);
    			removeChildrenComponent.setEnabled(true);
    		}
    		else {
    			JComponent removeChildrenComponent = (JComponent) popup.getComponent(13);
    			removeChildrenComponent.setEnabled(false);    		
    		}
        }
     }
}
